package org.way.iam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.way.base.dto.GlobalDTO;
import org.way.base.dto.PageParam;
import org.way.iam.entity.SysUserEntity;
import org.way.iam.service.ISysUserService;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private ISysUserService userService;

    @ApiOperation("当前用户信息")
    @SaCheckLogin
    @GetMapping("/info")
    public SaResult info() {
        return SaResult.data(userService.getUser());
    }

    @ApiOperation("添加用户")
    @SaCheckRole("admin")
    @PostMapping("/add")
    public SaResult add(@RequestBody SysUserEntity user) {
        userService.save(user);
        return SaResult.ok();
    }

    @ApiOperation("查询用户")
    @SaCheckPermission("user:see")
    @GetMapping("/{userId}")
    public SaResult getUser(@PathVariable String userId) {
        return SaResult.data(userService.getById(userId));
    }

    @ApiOperation("分页")
    @GetMapping("/page")
    @SaCheckPermission("user:see")
    public GlobalDTO<List<SysUserEntity>> page(PageParam param) {
        IPage<SysUserEntity> page = userService.page(param);
        return GlobalDTO.page(page.getRecords(),page.getTotal());
    }

    @ApiOperation("游客访问")
    @SaIgnore
    @GetMapping("/visitor")
    public SaResult visitor() {
        return SaResult.ok("visitor");
    }
}
